#!/bin/sh

#annotated by kavinyao@gmail.com

. /etc/init.d/functions

VERSION="$(basename $0) ver. 0.91"
USAGE="Usage: $(basename $0) < option > | --status-all | \
[ service_name [ command | --full-restart ] ]"
SERVICE=
SERVICEDIR="/etc/init.d"
OPTIONS=

# if no argument, print usage to stderr
# and exit with error code 1
if [ $# -eq 0 ]; then
   echo "${USAGE}" >&2
   exit 1
fi

# change to root directory (to ensure portability?)
cd /
# argument check, notice that $# with decrease by 1
# every time the while loop is executed as there is
# a shift at bottom
while [ $# -gt 0 ]; do
  case "${1}" in
    --help | -h | --h* )
       # print usage
       # and exit normally
       echo "${USAGE}" >&2
       exit 0
       ;;
    --version | -V )
       # print version
       # and exit normally
       echo "${VERSION}" >&2
       exit 0
       ;;
    --ignore-dependencies)
       # set SYSTEMCTL_IGNORE_DEPENDENCIES
       # and shift argument list
       export SYSTEMCTL_IGNORE_DEPENDENCIES=1
       shift
       ;;
    --skip-redirect)
       # set SYSTEMCTL_SKIP_REDIRECT
       # and shift argument list
       export SYSTEMCTL_SKIP_REDIRECT=1
       shift
       ;;
    *)
       # list status of all services
       if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
	  # change to service dir (/etc/init.d)
          cd ${SERVICEDIR}
          for SERVICE in * ; do
            case "${SERVICE}" in
	      # ignore some scripts should
              functions | halt | killall | single| linuxconf| kudzu)
                  ;;
              *)
		# make sure the file is ignored and the file is executable
                if ! is_ignored_file "${SERVICE}" \
		    && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
		  # execute in limited environment
                  env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" status
                fi
                ;;
            esac
          done
	  # and exit nomarlly
          exit 0
       # in this case, full start specified service
       elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
	  # set SERVICE to the first argument
          SERVICE="${1}"
          # make sure the file is ignored and the file is executable
          if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
            # stop service in limited environment
            env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" stop
            # start service in limited environment
            env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" start
	    # then exit with the return value of env
            exit $?
          fi
       # SERVICE is still null, set it to the first argument
       elif [ -z "${SERVICE}" ]; then
         SERVICE="${1}"
       # must be options
       else
         OPTIONS="${OPTIONS} ${1}"
       fi
       shift
       ;;
   esac
done

# now, should be the normal situation (start/stop/restart a service)
# sure, the service file must exist
if [ -f "${SERVICEDIR}/${SERVICE}" ]; then
   # execute in limited environment, with added environment variables
   env -i PATH="$PATH" TERM="$TERM" SYSTEMCTL_IGNORE_DEPENDENCIES=${SYSTEMCTL_IGNORE_DEPENDENCIES} SYSTEMCTL_SKIP_REDIRECT=${SYSTEMCTL_SKIP_REDIRECT} "${SERVICEDIR}/${SERVICE}" ${OPTIONS}
#if not, delegate to systemctl
else
   # write error message to stderr (why not use $2?)
   [ -c /dev/stderr ] && echo $"Redirecting to /bin/systemctl ${OPTIONS} ${SERVICE}.service" >/dev/stderr
   exec /bin/systemctl ${OPTIONS} ${SERVICE}.service
fi
